home *** CD-ROM | disk | FTP | other *** search
- // •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
- // MsgTest.c
- //
- // July 31, 1996
- // By Ben Manuto
- //
- // © 1996 by Apple Computer, Inc., all rights reserved.
- //
- // •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
-
-
-
- #include <Types.h>
- #include <LowMem.h>
- #include "Messaging.h"
- #include "MsgTest.h"
- #include "MsgUtil.h"
-
-
- // •••••••••• Globals
-
- MsgReceiveUPP uppMsgHandler;
- MsgCompletionUPP uppSendCompletion;
- MsgCompletionUPP uppReceiveCompletion;
-
- extern Boolean gSendCompCalled;
- extern Boolean gRcvCompCalled;
- extern Boolean gAckReceived;
- extern SInt32 gBytesReceived;
-
-
- // ••••••••••••••••••••••••
-
- MsgRecElemPtr NewMsgRecElem(SInt16 cmdBase, UInt16 cmdCount, UInt32 refCon)
- {
- MsgRecElemPtr theMsgRecElem;
-
- theMsgRecElem = (MsgRecElemPtr) NewPtrClear(sizeof(MsgRecElem));
-
- theMsgRecElem->recFlags = 0;
- theMsgRecElem->recProc = (MsgReceiveUPP) uppMsgHandler;
- theMsgRecElem->recCmdBase = cmdBase;
- theMsgRecElem->recCmdCount = 2; // Number of messages types the receive proce can accept.
- theMsgRecElem->recUserData = refCon;
-
- return(theMsgRecElem);
- }
-
-
- // ••••••••••••••••••••••••
-
- MsgPBlkPtr NewMessage(SInt16 cmdValue, UInt32 param1, UInt32 param2, UInt32 msgSize)
- {
- MsgPBlkPtr theMsg;
- SInt32 i;
-
- theMsg = (MsgPBlkPtr) NewPtrClear(sizeof(MsgPBlk));
- if (MemError()) return 0L; // If there was an error allocating space, return NIL.
-
- theMsg->msgCmd = cmdValue;
- theMsg->msgParam1 = param1;
- theMsg->msgParam2 = param2;
-
- if (msgSize > 0) {
- theMsg->msgBuffer = NewPtrClear(msgSize);
- if (MemError()) return 0L; // If there was an error allocating space, return NIL.
- theMsg->msgReqCount = msgSize;
-
- for (i = 0; i < msgSize; i++) // Fill the buffer with sequentially incrementing bytes.
- ((UInt8*)(theMsg->msgBuffer))[i] = i % 256;
- }
-
- theMsg->msgCompletion = (MsgCompletionUPP) uppSendCompletion;
- theMsg->msgUserData = 0L;
- theMsg->msgFlags = 0;
-
- return(theMsg);
- }
-
-
-
- // ••••••••••••••••••••••••
-
- void DeleteMessage(MsgPBlkPtr theMsg)
- {
- DisposePtr(theMsg->msgBuffer);
- DisposePtr((Ptr) theMsg);
- }
-
-
- // ••••••••••••••••••••••••
-
- void DeleteMsgRecElem(MsgRecElemPtr theMsgRecElem)
- {
- DisposePtr((Ptr) theMsgRecElem);
- }
-
-
- // ••••••••••••••••••••••••
-
- MsgPBlkPtr MessageHandler(MsgRecElemPtr theRecElem,
- UInt16 msgCmd,
- UInt32 param1,
- UInt32 param2)
- {
- MsgPBlkPtr theMsg;
- SInt16 i;
-
- if ((theRecElem->recCmdBase + kAckMsgType) == msgCmd) { // Is this an Ack message?
- // •••• Normally you could place checks here on param1 to see if the data packet to the PC was complete.
- // For our ack, we define param1 to contain the number bytes actually received by the PC.
-
- gBytesReceived = param1; // Set the global for number of bytes the PC received.
- gAckReceived = true; // set the global.
- return(0L); // Return nil and bail.
- }
-
- theMsg = (MsgPBlkPtr) (theRecElem->recUserData);
-
- for (i = 0; i < theMsg->msgParam2; i++) { // Clear the buffer for any new data to receive.
- ((UInt8*)(theMsg->msgBuffer))[i] = 0;
- }
-
- theMsg->msgActCount = 0;
- theMsg->msgCompletion = (MsgCompletionUPP) uppReceiveCompletion;
- theMsg->msgUserData = 0L;
- theMsg->msgFlags = 0L;
-
- return(theMsg);
- }
-
-
- // ••••••••••••••••••••••••
-
- MsgPBlkPtr SendCompletion(MsgPBlkPtr theMsg)
- {
- gSendCompCalled = true;
-
- return(theMsg);
- }
-
-
- // ••••••••••••••••••••••••
-
- MsgPBlkPtr RcvCompletion(MsgPBlkPtr theMsg)
- {
- gRcvCompCalled = true;
-
- return (theMsg);
- }
-
-
- // •••••••••••••••••••••••••
-
- OSErr InitializeMsgSystem(void)
- {
- OSErr error = noErr;
-
- uppSendCompletion = NewMsgCompletionProc(SendCompletion);
- uppMsgHandler = NewMsgReceiveProc(MessageHandler);
- uppReceiveCompletion = NewMsgCompletionProc(RcvCompletion);
-
- error = InitMessageSupport();
-
- return error;
- }
-